# gem install activerecord
# gem install sqlite3-ruby (sqlite3-ruby 1.2.3 (mswin32))

require 'bzcommon'
require 'rubygems'
require 'active_record'


include Bzflag
include BZCommon


ActiveRecord::Base.establish_connection(
      :adapter => "sqlite3",
      :dbfile  => "yabzs.sqlite"
 )

puts 0

ActiveRecord::Schema.define do
        create_table :logs do |table|
            table.column :line, :string
        end
end


$logEnabled = false;

class DatabaseLogger < Plugin

  def initialize()
    line = "\n------------ SERVER START [ #{@date} #{@time} ] ------------ "
    log line    
  end

  def log(line)
    puts line
    
  end
  
  def ctf_capture(data) 
  end
  
  def player_die(data)
    if $logEnabled && !is_server_player(data.killer_id)
      killer = Player.new(data.killer_id)
      victim = Player.new(data.player_id)
      flagCarried = data.flag_killed_with

      if data.killer_id == data.player_id
       line = "*** '#{killer.callsign}' blew myself up."
       log line 
      else
        flag = "no"
        if (!flagCarried.empty?) 
          flag = flagCarried
        end     
        line = "*** '#{victim.callsign}' destroyed by '#{killer.callsign}' who carried -#{flag}- flag."
        log line 
      end

      if is_score_max(killer) 
           game_end_function
      end
            
    end
  end
  
  def player_spawn(data)
  end
  
  def chat(data)
    
  end
  
  def player_join(data)
    line = "*** '#{data.callsign}' joined the game as #{get_team_color(data.team)}."
    log line 
  end
  
  def player_part(data)
    line = "*** '#{data.callsign}' left the game."
    log line
  end
  
  def unknown_slash_command(data)
  end
  
  def get_player_spawn_pos(data)
  end
  
  def allow_player(data)
  end

  def game_start(data)
    $logEnabled = true;
    line = "*** Game started."
    log line  
  end
  
  def game_end_function
    $logEnabled = false;
      
    winner = get_winner   
    ids = bz_get_player_index_list
    
    line = "*** Game ended."    
    log line
    line = "*** '#{winner.callsign}' has won!"
    log line
    line = "*** Game players"
    log line    
    ids.each do |id|
        player = Player.new(id)
        line = "*** Player - '#{player.callsign}'"
        log line     
    end
    
  end
  
  
  def game_end(data)
      game_end_function
  end
  
  def tick(data)
  end
  
  def generate_world(data)
  end
  
  def get_player_info(data)
  end
  
  def get_auto_team(data)
  end
  
  def allow_spawn_data(data)
  end
  
  def list_server_update(data)
  end
  
  def ban(data)
  end
  
  def host_ban(data)
  end
  
  def kick(data)
  end
  
  def kill(data)
    # kiedy jest wywolywany?    
  end
  
  def player_pause(data)
  end
  
  def slash_command(data)
  end
  
  def server_msg(data)
  end
  
  def shot_fired(data)
  end
  
  def shot_ended(data)
  end
  
  def flag_grabbed(data)
  end
  
  def flag_dropped(data)
  end

  def destructor()
    line = "\n------------ SERVER END [ #{@date} #{@time} ] ------------"
    log line  
      
  end

end

# new FileLogger instance
filelogger = DatabaseLogger.new()

# register FileLogger plugin
filelogger.register

puts "test"